﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using StockFinder.Model;
using StockFinder.NorgatePremiumData;

namespace StockFinder.Applications.Main
{
    public partial class UIControlsForm : Form
    {
        public UIControlsForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
                                                       
        }

        private void PaintDirectly()
        {
            Graphics graphics = this.CreateGraphics();

            //get the prices
            var symbol = new Symbol { Name = "AAPL" };
            var prices = new NorgatePremiumDataStockFilePathsExtractor().GetStockPriceHistoryFromFilename(
                @"C:\Trading Data\Formatted Output\NASDAQ\AAPL.csv").Take(1000);

            Pen Bluepen = new Pen(Color.Blue, 3);
            Pen redpen = new Pen(Color.Red, 3);
            Pen blackPen = new Pen(Color.Black, 3);

            const float START_X = 50;
            const float START_Y = 50;
            const float WIDTH = 1000;
            const float HEIGHT = 500;

            const int CANVAS_WIDTH = 1000;
            const int CANVAS_HEIGHT = 500;

            Bitmap canvas = new Bitmap(CANVAS_WIDTH, CANVAS_HEIGHT);

            float maxPrice = ((float)prices.Max(p => p.AdjustedClose));
            var maxYLabel = Math.Ceiling(maxPrice * 1.2f);

            var pixelsPerDollar = HEIGHT / maxYLabel;

            //y axis - same x
            graphics.DrawLine(Bluepen,
                    START_X, START_Y,
                    START_X, START_Y + HEIGHT);

            //x axis - same y
            graphics.DrawLine(redpen,
                    START_X, START_Y + HEIGHT,
                    START_X + WIDTH, START_Y + HEIGHT);

            DailyPrice price = null;
            float previousXValue = 0f;
            float previousYValue = 0f;

            //draw
            for (int i = 0; i < prices.Count(); i++)
            {
                price = prices.ElementAt(i);

                var yvalue = Convert.ToSingle(
                    (pixelsPerDollar * (double)price.AdjustedClose) + START_Y);

                if (i == 0)
                {
                    graphics.DrawLine(blackPen,
                        (START_X + i), yvalue,
                        (START_X + i), yvalue);
                }
                else
                {
                    graphics.DrawLine(blackPen,
                        previousXValue, previousYValue,
                        START_X + i, yvalue);
                }

                previousXValue = (START_X + i);
                previousYValue = yvalue;
            }
        }

        private void PaintViaBitmap()
        {            
            //get the prices
            var symbol = new Symbol { Name = "AAPL" };
            var prices = new NorgatePremiumDataStockFilePathsExtractor().GetStockPriceHistoryFromFilename(
                @"C:\Trading Data\Formatted Output\NASDAQ\AAPL.csv").Take(1000);

            Pen Bluepen = new Pen(Color.Blue, 3);
            Pen redpen = new Pen(Color.Red, 3);
            Pen blackPen = new Pen(Color.Black, 3);

            const float WIDTH = 1000;
            const float HEIGHT = 500;

            const int CANVAS_WIDTH = 1000;
            const int CANVAS_HEIGHT = 500;

            Bitmap canvas = new Bitmap(CANVAS_WIDTH, CANVAS_HEIGHT);

            var graphics = Graphics.FromImage(canvas);

            float maxPrice = ((float)prices.Max(p => p.AdjustedClose));
            var maxYLabel = Math.Ceiling(maxPrice * 1.2f);

            var pixelsPerDollar = HEIGHT / maxYLabel;

            //y axis - same x
            graphics.DrawLine(Bluepen,
                    0, 0,
                    0, HEIGHT);

            //x axis - same y
            graphics.DrawLine(redpen,
                    0, HEIGHT,
                    WIDTH, HEIGHT);

            DailyPrice price = null;
            float previousXValue = 0f;
            float previousYValue = 0f;

            //draw
            for (int i = 0; i < prices.Count(); i++)
            {
                price = prices.ElementAt(i);

                var yvalue = Convert.ToSingle(
                    (pixelsPerDollar * (double)price.AdjustedClose));

                if (i == 0)
                {
                    graphics.DrawLine(blackPen,
                        (i), yvalue,
                        (i), yvalue);
                }
                else
                {
                    graphics.DrawLine(blackPen,
                        previousXValue, previousYValue,
                        i, yvalue);
                }

                previousXValue = (i);
                previousYValue = yvalue;
            }

            _ChartPictureBox.Image = canvas;
        }

        private void UIControlsForm_Paint(object sender, PaintEventArgs e)
        {
            PaintViaBitmap();            
        }
    }
}
